/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import ai_game.AIGame;
import java.util.ArrayList;
import java.util.List;
import org.newdawn.slick.geom.Point;
import org.newdawn.slick.geom.Vector2f;
import physics.CollisionClass;


/**
 *
 * @author ant
 */
public class Enemy1 extends GameObject implements AI_Agent
{
    public static final int wallFeelerLength = 75;
    private ArrayList<Point> patrolWaypoints; 
    
    //sense data
    List<GameObject> radarResult;
    double [] wallFeelers;
    double [] activationLevels;
    final float maxSpeed = 10;
    final float radarRadius = 150;
    final float maxAcel = 5;
    
    private GameObject seekingObject;
    
    //the new acceleration to apply
    private Vector2f desiredAccel;

    //alternate version for sentry behavior
//    enum State
//    {
//        walkingPatrolSegment,
//        stoppedAtWaypoint, //in between segments.
//        seek, //follow player
//    }
    public Enemy1(){
        collisionClass=CollisionClass.enemy;
    }
    
    enum State
    {
        idle,
        seek
    }
    
    State state = State.idle;
    
    @Override
    public void sense()
    {
        radarResult = AIGame.game.physicsEngine.getWithinRadius(pos, radarRadius, CollisionClass.player);
        //wallFeelers = feelWalls(Math.PI/4, wallFeelerLength);
        wallFeelers = new double[4];
        activationLevels = AIGame.game.physicsEngine.pieSliceQuadSensor(pos, radarRadius, (float) headingRadians, CollisionClass.player); 
        //TODO filter list of results to only see what's in front.
    }

    @Override
    public void think()
    {
        switch(state)
        {
            //for now just assume there will only be one result
            case idle:
                if(!radarResult.isEmpty())
                {
                    seekingObject = radarResult.get(0);
                    state = State.seek;
                }
                break;
            case seek:
            {
                Vector2f desiredVel = pos.negate().add(seekingObject.pos).scale(radarRadius);
                desiredAccel = vel.negate().add(desiredVel);
                
                //scale acceleration to within limits.
                float accNorm = desiredAccel.length();
                if(accNorm > maxAcel)
                    desiredAccel.scale(maxAcel/accNorm);
            }
        }
    }

    @Override
    public void act()
    {
//        acc = desiredAccel;
    }

    @Override
    public double headingRadians() {
        return headingRadians;
    }
    
    //behavior specific to this class:
    //set sprite/animation direction based on the heading this frame
    @Override
    public void update()
    {
        if(vel.lengthSquared() > 0)
        {
            headingRadians = Math.atan2(vel.y, vel.x);
        }
        //int dir = (int) ((headingRadians+Math.PI/4)/Math.PI/2);
        int dir = (int) ((headingRadians+Math.PI/4)/(Math.PI/2));

        //if odd numbered direction (vertical) flip to resolve y-axis 
        //convention
        dir += (dir%2)*2;
        
        if(dir < 0) dir += 4;
        else if(dir >= 4) dir -= 4;
        
        animation.setDir(dir);
    }
    
    public void setInitialState(double rads){
        headingRadians=rads;
    }
    
    String string(){
        return ""+headingRadians;
    }
    
}
